package com.shopManager.Interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;

public class SessionCheckInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
//		boolean isAjaxRequest = false;  
//        if(!StringUtils.isBlank(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equals("XMLHttpRequest")){  
//            isAjaxRequest = true;  
//        }  
//		response.setContentType("text/html;charset=utf-8");
//		if (request.getSession().getAttribute("user") == null) {
//			if(isAjaxRequest){
//                PrintWriter writer = response.getWriter();  
//                writer.write(JSON.toJSONString(RestResultGenerator.genErrorResult("用户未登录", null, Status.login_error)));  
//                writer.close();  
//			}else{
//				response.sendRedirect(""+request.getContextPath()+"/login.do");  
//			}
//			
//			return false;
//		}
		return true;
	}

	
}
